//
//  UIPopView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/17.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

class UIPopView: UIView {
    
    enum PopType {
        case launch
        case pop
        case fadeIn
    }
    var offset:CGFloat = 0.0
    weak var showView:UIView?
    var bgControl:UIControl?
    private var popType:PopType = .launch
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
    init(){
        super.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - OFFSET_BOTTOM))
        setting()
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setting()
    }
    
    func setting()  {
        
        self.bgControl = UIControl.init(frame:  CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - OFFSET_BOTTOM))
        self.autoresizing(index: 0)
        self.bgControl?.backgroundColor = RGBA(r: 0, g: 0, b: 0, a: 0.5)
        self.alpha = 0
        self.bgControl?.addTarget(self, action: #selector(bgTapAction), for: .touchUpInside)
        self.addSubview(self.bgControl!)
        //        self.setShadow(color: UIColor.black, offset: CGSize.init(width: 0, height: -3), opacity: 0.8, radius: 3)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func bgTapAction() {
        if self.popType == .launch {
            self.close({
                
            })
        }else{
        }
    }
    
    func show(_ view:UIView,type:PopType) {
        
        self.show(view, type: type) {
            
        }
    }
    
    func show(_ view:UIView,type:PopType, finsh:@escaping(()->())) {
        
        self.popType = type
        if self.popType == .launch {
            self.launchShow(view,finsh:finsh)
        }else{
            self.popShow(view,finsh:finsh)
        }
    }
    
    
    func close(_ back: @escaping () -> ()) {
        
        if self.popType == .launch {
            self.launchClose(back)
        }else{
            self.popClose(back)
        }
    }
    
    fileprivate func launchClose(_ back: @escaping () -> ()) {
        
        weak var weakSelf = self
        UIApplication.shared.keyWindow?.addSubview(self)
        
        UIView.animate(withDuration: 0.3, animations: {
            weakSelf?.bgControl?.backgroundColor = RGBA(r: 0, g: 0, b: 0, a: 0)
            weakSelf?.showView?.frame = CGRect.init(x: (VIEW_WIDTH(view: weakSelf!) - VIEW_WIDTH(view: (weakSelf?.showView)!))/2 , y:VIEW_HEIGHT(view: weakSelf!) , width: VIEW_WIDTH(view: (weakSelf?.showView)!), height: VIEW_HEIGHT(view: (weakSelf?.showView)!))
            
        }) { (true) in
            weakSelf?.frame = CGRect.init(x: 0, y: SCREEN_HEIGHT - OFFSET_BOTTOM, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - OFFSET_BOTTOM)
            weakSelf?.showView?.removeFromSuperview()
            weakSelf?.alpha = 0
            back()
        }
        
    }
    
    fileprivate func launchShow(_ view:UIView){
        
        self.launchShow(view) {
            
        }
    }
    
    fileprivate func launchShow(_ view:UIView,finsh:@escaping(()->())){
        
        self.alpha = 1
        view.frame = CGRect.init(x: (VIEW_WIDTH(view: self) - VIEW_WIDTH(view: view))/2 , y:VIEW_HEIGHT(view: self) , width: VIEW_WIDTH(view: view), height: VIEW_HEIGHT(view: view))
        showView = view
        self.addSubview(view)
        UIApplication.shared.keyWindow?.addSubview(self)
        weak var weakSelf = self
        self.frame = CGRect.init(x: 0, y: SCREEN_HEIGHT - OFFSET_BOTTOM, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - OFFSET_BOTTOM)
        weakSelf?.frame = CGRect.init(x: 0, y:0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - OFFSET_BOTTOM)
        
        UIView.animate(withDuration: 0.25, animations: {
            view.frame = CGRect.init(x: (VIEW_WIDTH(view: self) - VIEW_WIDTH(view: view))/2 , y:VIEW_HEIGHT(view: self) - VIEW_HEIGHT(view: view), width: VIEW_WIDTH(view: view), height: VIEW_HEIGHT(view: view))
            weakSelf?.bgControl?.backgroundColor = RGBA(r: 0, g: 0, b: 0, a: 0.4)
            
        }) { (com) in
            finsh()
        }
    }
    
    
    fileprivate func popClose(_ back: @escaping () -> ()) {
        
        weak var weakSelf = self
        UIApplication.shared.keyWindow?.addSubview(self)
        
        UIView.animate(withDuration: 0.3, animations: {
            weakSelf?.alpha = 0
        }) { (true) in
            weakSelf?.showView?.removeFromSuperview()
            weakSelf?.alpha = 0
            back()
            
        }
        
    }
    
    fileprivate func popShow(_ view:UIView){
        self.popShow(view) {
            
        }
    }
    
    fileprivate func popShow(_ view:UIView,finsh:@escaping(()->())){
        
        self.alpha = 1
        view.frame = CGRect.init(x:(VIEW_WIDTH(view: self) - VIEW_WIDTH(view: view))/2, y:(VIEW_HEIGHT(view: self) - VIEW_HEIGHT(view: view))/2 + offset, width: VIEW_WIDTH(view: view), height: VIEW_HEIGHT(view: view))
        showView = view
        self.addSubview(view)
        UIApplication.shared.keyWindow?.addSubview(self)
        
        weak var weakSelf = self
        self.frame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - OFFSET_BOTTOM)
        
        UIView.animate(withDuration: 0.3, animations: {
            weakSelf?.alpha = 1
            weakSelf?.bgControl?.backgroundColor = RGBA(r: 0, g: 0, b: 0, a: 0.5)
        }) { (end) in
            finsh()
        }
        
    }
    
    fileprivate func fadeInShow(_ view:UIView,finsh:@escaping(()->())){
        let showWidth = VIEW_WIDTH(view: view)
        let showHeight = VIEW_HEIGHT(view: view)
        self.alpha = 0
        view.frame = CGRect.init(x:VIEW_WIDTH(view: self)/2, y:VIEW_HEIGHT(view: self)/2 + offset, width: 0, height: 0)
        showView = view
        showView?.clipsToBounds = true
        self.addSubview(view)
        UIApplication.shared.keyWindow?.addSubview(self)
        
        weak var weakSelf = self
        self.frame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - OFFSET_BOTTOM)
        
        UIView.animate(withDuration: 0.3, animations: {
            weakSelf?.alpha = 1
            weakSelf?.bgControl?.backgroundColor = RGBA(r: 0, g: 0, b: 0, a: 0.5)
            weakSelf?.showView?.frame = CGRect(x:(VIEW_WIDTH(view: weakSelf!) - showWidth)/2, y:(VIEW_HEIGHT(view: weakSelf!) - showHeight)/2 + (weakSelf?.offset)!, width: showWidth, height: showHeight)
        }) { (end) in
            finsh()
        }
        
    }
    
    fileprivate func fadeInClose(_ back: @escaping () -> ()) {
        
        weak var weakSelf = self
        UIApplication.shared.keyWindow?.addSubview(self)
        
        UIView.animate(withDuration: 0.3, animations: {
            weakSelf?.alpha = 0
        }) { (true) in
            weakSelf?.showView?.removeFromSuperview()
            weakSelf?.alpha = 0
            back()
            
        }
        
    }
    
}
